package com.gjy.pd.decorator;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-12-23 09:39:22
 */
public class Client {

    public static void main(String[] args) {
        Component c = new ConcreteComponent();

        Decorator da = new ConcreteDecoratorA(c);
        da.operator();

        Decorator db = new ConcreteDecoratorB(c);
        db.operator();

        Decorator dd = new ConcreteDecoratorB(da);
        dd.operator();
    }

    private static abstract class Component {
        public abstract void operator();
    }

    private static class ConcreteComponent extends Component {

        @Override
        public void operator() {
            System.out.println("处理业务逻辑");
        }
    }

    private abstract static class Decorator extends Component {
        private final Component component;

        public Decorator(Component component) {
            this.component = component;
        }

        @Override
        public void operator() {
            component.operator();
        }
    }

    private static class ConcreteDecoratorA extends Decorator {

        public ConcreteDecoratorA(Component component) {
            super(component);
        }

        private void first() {
            System.out.println(this.getClass().getName() + "first");
        }

        private void last() {
            System.out.println(this.getClass().getName() + "last");
        }

        @Override
        public void operator() {
            first();
            super.operator();
            last();
        }
    }

    private static class ConcreteDecoratorB extends Decorator {

        public ConcreteDecoratorB(Component component) {
            super(component);
        }

        private void first() {
            System.out.println(this.getClass().getName() + "first");
        }

        private void last() {
            System.out.println(this.getClass().getName() + "last");
        }

        @Override
        public void operator() {
            first();
            super.operator();
            last();
        }
    }
}
